package com.linghu.influx;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;

import java.util.List;
import java.util.Map;

/**
 * @author 令狐荣豪
 * @version 1.0
 */
public class QuerySample {

    public static void main(String[] args) {
        char[] token= "QTEG3Rpx6ErCFDypxdqMvhXXuh-Ot2cxkFvcLE1mr6Q_4DsE0AhYxq9u10y0EG2UGRcL_n7JAi902ITMnF1hRA==".toCharArray();
        String bucket = "linghu";
        String org = "haomeng";

        InfluxDBClient client = InfluxDBClientFactory.create("http://192.168.28.128:8086", token,org,bucket);
        QueryApi queryApi = client.getQueryApi();

        String flux="from(bucket: \"linghu\")\n" +
                "  |> range(start: -12h)\n" +
                "  |> filter(fn: (r) => r[\"_measurement\"] == \"tempeature\")\n" +
                "  |> filter(fn: (r) => r[\"_field\"] == \"value\")\n" +
                "  |> aggregateWindow(every: 10s, fn: mean, createEmpty: false)\n" +
                "  |> yield(name: \"mean\")";

        String data = queryApi.queryRaw(flux);//csv格式数据，一般不用
        
        List<FluxTable> query = queryApi.query(flux);
        for (FluxTable fluxTable : query) {
            List<FluxRecord> records = fluxTable.getRecords();
            for (FluxRecord record : records) {
//                System.out.println(record.getValueByKey("_value"));
                /** 获取所有的记录值 */
                Map<String, Object> values = record.getValues();
                System.out.println(values);
                Object reasult = values.get("location");
                System.out.println(reasult);
            }
        }
    }
}
